﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Services;
using System.Security.Cryptography;

namespace Encrypt
{
    /// <summary>
    /// Encyrpt and Decrypt the informatin used by the Control de Peso windows application.
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    
    public class EncryptControlDePeso : System.Web.Services.WebService
    {
        static string key = "POO";

        [WebMethod]
        public string Encrypt(string texto)
        {
            try
            {
                byte[] keyArray;

                byte[] Arreglo_a_Cifrar =
                UTF8Encoding.UTF8.GetBytes(texto);

                MD5CryptoServiceProvider hashmd5 =
                new MD5CryptoServiceProvider();

                keyArray = hashmd5.ComputeHash(
                UTF8Encoding.UTF8.GetBytes(key));

                hashmd5.Clear();

                //Algoritmo 3DAS
                TripleDESCryptoServiceProvider tdes =
                new TripleDESCryptoServiceProvider();

                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform =
                tdes.CreateEncryptor();

                byte[] ArrayResultado =
                cTransform.TransformFinalBlock(Arreglo_a_Cifrar,
                0, Arreglo_a_Cifrar.Length);

                tdes.Clear();

                return Convert.ToBase64String(ArrayResultado,
                0, ArrayResultado.Length);
            }
            catch
            {
                return null;
            }
        }

        [WebMethod]
        public string Decrypt(string textoEncriptado)
        {
            try
            {
                byte[] keyArray;
                //convierte el texto en una secuencia de bytes
                byte[] Array_a_Descifrar =
                Convert.FromBase64String(textoEncriptado);

                MD5CryptoServiceProvider hashmd5 =
                new MD5CryptoServiceProvider();

                keyArray = hashmd5.ComputeHash(
                UTF8Encoding.UTF8.GetBytes(key));

                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes =
                new TripleDESCryptoServiceProvider();

                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform =
                tdes.CreateDecryptor();

                byte[] resultArray =
                cTransform.TransformFinalBlock(Array_a_Descifrar,
                0, Array_a_Descifrar.Length);

                tdes.Clear();

                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch
            {
                return null;
            }
        }
    }
}
